package cn.edu.scau.cmi.mingjunhui.client;

import java.util.Scanner;

import cn.edu.scau.cmi.mingjunhui.composite.consistent.ConsistentComponent;
import cn.edu.scau.cmi.mingjunhui.composite.consistent.ConsistentComposite;
import cn.edu.scau.cmi.mingjunhui.composite.consistent.ConsistentLeaf;

public class ConsistentCompositeClient {
	public static void main(String[] args)
	{
		
		ConsistentComponent coca = new ConsistentComposite(null, "可口可乐");
		ConsistentComponent meizhiyuan = new ConsistentLeaf(coca, "美汁源");
		coca.addChild(meizhiyuan);
		
		ConsistentComponent cola = new ConsistentComposite(coca, "碳酸饮料");
		coca.addChild(cola);
		ConsistentComponent fenta = new ConsistentLeaf(cola, "芬达");
		cola.addChild(fenta);
		ConsistentComponent xuebi = new ConsistentLeaf(cola, "雪碧");
		cola.addChild(xuebi);
		ConsistentComponent jianyi = new ConsistentLeaf(cola, "健怡可乐");
		cola.addChild(jianyi);
		
		//ConsistentComponent a = coca.get("碳酸饮料");
		//System.out.println(a);
		
		System.out.println(coca);
		
		Scanner input = new Scanner(System.in);
		while(true) {
			System.out.println("1:查看组织结构\n2:添加节点\n3:删除节点\n");
			int op = input.nextInt();
			if(op==1) {
				System.out.println(coca);
			}
			else if(op==2){
				System.out.println("请输入你要添加节点的类型：个人/组织");
				String type = input.next();
				System.out.println("请输入你要加入的节点名称");
				String tarName = input.next();
				ConsistentComponent newPerson = null;  ConsistentComponent newTeam = null;
				System.out.println("请输入你要添加节点的名称");
				String newName = input.next();
				if(type.equals("个人")) {
					newPerson = new ConsistentLeaf(null, newName);
					//System.out.println("!!!!!!!!!!");
					newPerson.son.clear();
				}
				else {
					newTeam = new ConsistentComposite(null, newName);
					newTeam.son.clear();
				}
				ConsistentComponent target = coca.get(tarName);
				//System.out.println(target.name);
				//System.out.println(tarName);
				//ConsistentComponent a = coca.get("芬达");
				//System.out.println(a);
				if(target==null) System.out.println("该节点无法加入");
				else {
					if(type.equals("个人")) {
						newPerson.parent = target;
						target.addChild(newPerson);
						newPerson.son.clear();
					}
					else {
						newTeam.parent = target;
						target.addChild(newTeam);
						newTeam.son.clear();
					}
				}
			}
			else {
				System.out.println("请输入你要删除节点的类型：个人/组织");
				String type = input.next();
				System.out.println("请输入你要删除的节点名称");
				String tarName = input.next();
				ConsistentComponent newPerson = null;  ConsistentComponent newTeam = null;
				ConsistentComponent target = coca.get(tarName);
				if(target.getName().equals("学校")) System.out.println("该节点无法删除");
				else {
					target.parent.deleteChild(target);
				}
			}
			System.out.println();
		}
	}
}
